package dao

import (
	"month3/inits"
	"month3/model"
)

func Login(mobile string) (user model.User) {
	err := inits.DB.Where("mobile=?", mobile).Find(&user).Error
	if err != nil {
		return model.User{}
	}
	return user
}

func Register(mobile string) (user model.User) {
	user = model.User{
		Mobile: mobile,
	}
	err := inits.DB.Create(&user).Error
	if err != nil {
		return model.User{}
	}
	return user
}

func AddArticle(userId uint, context string, articleType int, goodNum int, commentNum int) (result model.Article) {
	result = model.Article{
		UserId:      int(userId),
		Context:     context,
		ArticleType: articleType,
		GoodNum:     goodNum,
		CommentNum:  commentNum,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.Article{}
	}
	return result
}

func List() (result []model.Article) {
	err := inits.DB.Order("good_num desc").Find(&result).Error
	if err != nil {
		return []model.Article{}
	}
	return result
}

func Focus(userId uint, focusId int) (result model.Focus) {
	result = model.Focus{
		UserId:  int(userId),
		FocusId: focusId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.Focus{}
	}
	return result
}

func List1(userId int) []model.Article {
	var result []model.Article
	err := inits.DB.Raw("SELECT * FROM article e LEFT JOIN (SELECT focus_id FROM focus WHERE user_id=?) a ON e.user_id=a.focus_id ORDER BY focus_id desc,good_num desc", userId).Scan(&result).Error
	if err != nil {
		return []model.Article{}
	}
	return result
}

func AddArticleLike(articleId int) (result model.ArticleLike) {
	result = model.ArticleLike{
		ArticleId: articleId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.ArticleLike{}
	}
	return result
}

func DelArticleLike(articleId int) bool {
	err := inits.DB.Where("article_id=?", articleId).Delete(&model.ArticleLike{}).Error
	if err != nil {
		return false
	}
	return true
}

var result1 model.Comment

func AddComment(articleId int, content string, userId int) model.Comment {
	result1 = model.Comment{
		UserId:    userId,
		ArticleId: articleId,
		Content:   content,
		FatherId:  0,
	}
	err := inits.DB.Create(&result1).Error
	if err != nil {
		return model.Comment{}
	}
	return result1
}

func AppendComment(articleId int, content string, userId int) model.Comment {
	result := model.Comment{
		UserId:    userId,
		ArticleId: articleId,
		Content:   content,
		FatherId:  result1.Id,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.Comment{}
	}
	return result1
}

func AddCommentLike(commentId int) model.CommentLike {
	result := model.CommentLike{CommentId: commentId}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.CommentLike{}
	}
	return result
}

func DelAddCommentLike(commentId int) bool {
	err := inits.DB.Where("comment_id=?", commentId).Delete(&model.CommentLike{}).Error
	if err != nil {
		return false
	}
	return true
}

func ArticleLikeList() (result []model.ArticleLike) {
	err := inits.DB.Find(&result).Error
	if err != nil {
		return []model.ArticleLike{}
	}
	return result
}

func CommentLikeList() (result []model.CommentLike) {
	err := inits.DB.Find(&result).Error
	if err != nil {
		return []model.CommentLike{}
	}
	return result
}

func AddRoom(userId int) bool {
	room1 := model.Room{
		RoomUserId: userId,
	}
	err := inits.DB.Create(&room1).Error
	room2 := model.RoomUser{
		RoomUserId: userId,
		ULevel:     1,
		RoomId:     room1.Id,
	}
	err = inits.DB.Create(&room2).Error
	if err != nil {
		return false
	}
	return true
}

func GetULevel(roomId int, level int) (result model.RoomUser) {
	err := inits.DB.Where("room_id=?", roomId).Where("u_level=?", level).Find(&result).Error
	if err != nil {
		return model.RoomUser{}
	}
	return result
}

func AddRoomUser(roomId int, roomUserId int) (result model.RoomUser) {
	result = model.RoomUser{
		RoomUserId: roomUserId,
		RoomId:     roomId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return model.RoomUser{}
	}
	return result
}

func DelRoomUser(roomId int, roomUserId int) (result model.RoomUser) {
	err := inits.DB.Where("room_id=?", roomId).Where("room_user_id=?", roomUserId).Delete(&result).Error
	if err != nil {
		return model.RoomUser{}
	}
	return result
}

func UpdateRoomUser(roomId int, roomUserId int) (result model.RoomUser) {
	result = model.RoomUser{
		RoomUserId: roomUserId,
		RoomId:     roomId,
		ULevel:     2,
	}
	err := inits.DB.Where("room_user_id=?", roomUserId).Where("room_id=?", roomId).Updates(&result).Error
	if err != nil {
		return model.RoomUser{}
	}
	return result
}

func DelRoom(roomUserId int) bool {
	err := inits.DB.Where("room_user_id=?", roomUserId).Delete(&model.Room{}).Error
	if err != nil {
		return false
	}
	return true
}
